#ifndef _BARTER_SERVER_NETWORKMESSAGE_H_
#define _BARTER_SERVER_NETWORKMESSAGE_H_

#include <string>

class NetworkMessage
{
private:
	/*
		Posistion: current posistion for reading in Buffer.
		Length: current position when writing and current length of Buffer.
		Alignment: when adding an item, keeps the buffer aligned
	*/
	unsigned int Position, Length, Alignment;
	unsigned char Buffer[32768];
	bool CanAdd(int size);
public:
	NetworkMessage();
	NetworkMessage(const NetworkMessage&);
	NetworkMessage(unsigned char* buf, int len);
	NetworkMessage& operator =(const NetworkMessage&); // assignment operator: msg1 = msg2;
	bool WriteByte(unsigned char);
	bool WriteWord(unsigned short);
	bool WriteDoubleWord(unsigned long);
	bool WriteQuadWord(unsigned long long);
	bool WriteRaw(const unsigned char* buffer, int length); // buffer must be at least `length` bytes long
	bool WriteString(std::string);
	unsigned char ReadByte();
	unsigned short ReadWord();
	unsigned long ReadDoubleWord();
	unsigned long long ReadQuadWord();
	int ReadRaw(unsigned char* buffer, int length); // return number of bytes read
	std::string ReadString();
	unsigned int GetLength();
	void Reset(); // empty the buffer and reset iterators
	NetworkMessage& Append(NetworkMessage&);
	int GetBuffer(unsigned char*, int); // output buffer, length of output buffer; returns number of bytes copied
	void Copy(NetworkMessage&, int); // msg, offset (from msg.Position)
};

#endif // _BARTER_SERVER_NETWORKMESSAGE_H_
